﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _10_HPBar.GameFrame;
using _10_HPBar.GameFrame.Interface;
using _10_HPBar.Scene;
using _10_HPBar.Tool;

namespace _10_HPBar.CharacterBase
{
    internal class Enemy : Character
    {
        long currentTime, lastTime;
        Pos backup;
        SceneMain scene;
        Random rand;

        public Enemy(string name, int hp, int mp, int atk, int def, Pos pos, Map map, string display, InfoSystem info, SceneMain scene)
            : base(name, hp, mp, atk, def, pos, map, display, info)
        {
            currentTime = lastTime = 0;
            this.scene = scene;
            rand = new Random();
        }

        void Move(int offsetX, int offsetY)
        {
            map.SetType(Pos, Map.Type.Empty);
            backup = Pos;
            Pos temp = Pos;
            Utility.MyPrint("  ", Pos.x + map.offsetX, Pos.y + map.offsetY);
            temp.x += offsetX;
            temp.y += offsetY;
            Pos = temp;
            if (map.GetType(Pos) == Map.Type.Wall ||
               map.GetType(Pos) == Map.Type.Box)
            {
                Pos = backup;
            }
            map.SetType(Pos, Map.Type.Enemy);
            // 如果和玩家重叠
            // 需要获得玩家位置
            //Character? player = scene.GetPlayer(pos);
            //if (player != null)
            //{
            //    // todo 打开战斗系统
            //    IsCombating = true;
            //    // 战斗系统添加战斗结束事件 用于恢复IsCombating
            //}
        }

        public void Update()
        {
            if (IsDialoging || IsCombating) return;

            currentTime = DateTime.Now.Ticks;
            if (currentTime - lastTime > Game.rand.Next(1000, 3000) * 10000)
            {
                Move(Game.rand.Next(-1, 2), Game.rand.Next(-1, 2));
                lastTime = DateTime.Now.Ticks;
            }
        }

        public override void OnDead()
        {
            base.OnDead();
            // 删除逻辑
            if(rand.Next(100) > 50)
               map.SetType(Pos, Map.Type.Item);
            scene.collision.Distroy(this);
            //Utility.MyPrint("  ", Pos.x + map.offsetX, Pos.y + map.offsetY);
        }
    }
}